package servlet;

import utils.DBConnectionUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DBConnectionUtil.init();
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String selectSql = "select * from user where name='" + name + "';";
        ResultSet rs = DBConnectionUtil.select(selectSql);
        try {
            if (rs.next()) {
                System.out.println("用户已存在");
            } else {
                String updateSql = "insert into user(name, password) values( '"+name+ "','" +password+"');";
                int i = DBConnectionUtil.update(updateSql);
                System.out.println(i);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
